


<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->

<!-- Mirrored from www.cyclismo.org/tutorial/matlab/plot.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 04:32:27 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8" /><!-- /Added by HTTrack -->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Plotting &mdash; Matlab Tutorial 3.0 documentation</title>
  

  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
  

  
    <link rel="stylesheet" href="_static/siteColours.css" type="text/css" />
  

  
    <link rel="top" title="Matlab Tutorial 3.0 documentation" href="index.html"/>
        <link rel="next" title="Executable Files" href="execute.html"/>
        <link rel="prev" title="Loops" href="control.html"/> 

  
  <script src="_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="index.html" class="icon icon-home"> Matlab Tutorial
          

          
          </a>

          
            
            
              <div class="version">
                3.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="https://www.cyclismo.org/tutorial/matlab/search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          

<div class="local-toc"><ul>
<li><a class="reference internal" href="#">Plotting</a></li>
</ul>
</div>
					


<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="vector.html">Introduction to Vectors in Matlab</a></li>
<li class="toctree-l1"><a class="reference internal" href="matrix.html">Introduction to Matrices in Matlab</a></li>
<li class="toctree-l1"><a class="reference internal" href="operations.html">Vector Functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="control.html">Loops</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Plotting</a></li>
<li class="toctree-l1"><a class="reference internal" href="execute.html">Executable Files</a></li>
<li class="toctree-l1"><a class="reference internal" href="subroutine.html">Subroutines</a></li>
<li class="toctree-l1"><a class="reference internal" href="if.html">The If Statement</a></li>
<li class="toctree-l1"><a class="reference internal" href="dataFile.html">Data Files</a></li>
</ul>


<div class="datacampAd">
  <span style="vertical-align:top;color: black;">
<center>
<script type="text/javascript"><!--
google_ad_client = "ca-pub-8301099787686694";
google_ad_width = 120;
google_ad_height = 240;
google_ad_type = "text_image";
google_ad_channel ="6171035172";
google_ad_slot = "4696657180";
google_color_border = "808080";
google_color_bg = "F0ECEC";
google_color_link = "000000";
google_color_url = "666666.html";
google_color_text = "000000";
//--></script>
<script type="text/javascript"
  src="../../../pagead2.googlesyndication.com/pagead/f.txt">
</script>
</center>
</span>
</div>


        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="index.html">Matlab Tutorial</a>
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          





<div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="index.html">Docs</a> &raquo;</li>
      
    <li>Plotting</li>
      <li class="wy-breadcrumbs-aside">
        
          
        
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="plotting">
<span id="id1"></span><h1>Plotting<a class="headerlink" href="#plotting" title="Permalink to this headline">¶</a></h1>
<p id="index-0">In this tutorial we will assume that you know how to create vectors
and matrices, know how to index into them, and know about loops. For
more information on those topics see one of our tutorials on vectors
(<a class="reference internal" href="vector.html#vectors"><span>Introduction to Vectors in Matlab</span></a>), matrices (<a class="reference internal" href="matrix.html#matrices"><span>Introduction to Matrices in Matlab</span></a>), vector operations
(<a class="reference internal" href="operations.html#vectoroperations"><span>Vector Functions</span></a>), or loops (<a class="reference internal" href="control.html#loops"><span>Loops</span></a>).</p>
<p>In this tutorial we will introduce the basic operations for creating
plots. To show how the <em>plot</em> command is used, an approximation using
Euler&#8217;s Method is found and the results plotted. We will approximate
the solution to the D.E. y&#8217;= 1/y, y(0)=1. A step size of <em>h=1/16</em> is
specified and Euler&#8217;s Method is used. Once done, the true solution is
specified so that we can compare the approximation with the true
value. (This example comes from the tutorial on loops (:ref`loops`).)</p>
<div class="highlight-matlab"><div class="highlight"><pre><span class="o">&gt;&gt;</span> <span class="n">h</span> <span class="p">=</span> <span class="mi">1</span><span class="o">/</span><span class="mi">16</span><span class="p">;</span>
<span class="o">&gt;&gt;</span> <span class="n">x</span> <span class="p">=</span> <span class="mi">0</span><span class="p">:</span><span class="n">h</span><span class="p">:</span><span class="mi">1</span><span class="p">;</span>
<span class="o">&gt;&gt;</span> <span class="n">y</span> <span class="p">=</span> <span class="mi">0</span><span class="o">*</span><span class="n">x</span><span class="p">;</span>
<span class="o">&gt;&gt;</span> <span class="nb">size</span><span class="p">(</span><span class="n">y</span><span class="p">)</span>

<span class="nb">ans</span> <span class="p">=</span>

     <span class="mi">1</span>    <span class="mi">17</span>

<span class="o">&gt;&gt;</span> <span class="n">max</span><span class="p">(</span><span class="nb">size</span><span class="p">(</span><span class="n">y</span><span class="p">))</span>

<span class="nb">ans</span> <span class="p">=</span>

    <span class="mi">17</span>

<span class="o">&gt;&gt;</span> <span class="n">y</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="p">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="o">&gt;&gt;</span> <span class="k">for</span> <span class="nb">i</span><span class="p">=</span><span class="mi">2</span><span class="p">:</span><span class="n">max</span><span class="p">(</span><span class="nb">size</span><span class="p">(</span><span class="n">y</span><span class="p">)),</span>
     <span class="n">y</span><span class="p">(</span><span class="nb">i</span><span class="p">)</span> <span class="p">=</span> <span class="n">y</span><span class="p">(</span><span class="nb">i</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="n">h</span><span class="o">/</span><span class="n">y</span><span class="p">(</span><span class="nb">i</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span>
   <span class="k">end</span>
<span class="o">&gt;&gt;</span> <span class="n">true</span> <span class="p">=</span> <span class="nb">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span>
</pre></div>
</div>
<p>Now, we have an approximation and the true solution. To compare the
two, the true solution is plotted with the approximation plotted at
the grid points as a green &#8216;o&#8217;. The <em>plot</em> command is used to generate
plots in matlab. There is a wide variety of arguments that it will
accept. Here we just want one plot, so we give it the range, the
domain, and the format.</p>
<div class="highlight-matlab" id="index-2"><div class="highlight"><pre><span class="o">&gt;&gt;</span> <span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="s">&#39;go&#39;</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">true</span><span class="p">)</span>
</pre></div>
</div>
<p>That&#8217;s nice, but it would also be nice to plot the error:</p>
<div class="highlight-matlab"><div class="highlight"><pre><span class="o">&gt;&gt;</span> <span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="nb">abs</span><span class="p">(</span><span class="n">true</span><span class="o">-</span><span class="n">y</span><span class="p">),</span><span class="s">&#39;mx&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Okay, let&#8217;s print everything on one plot. To do this, you have to tell
matlab that you want two plots in the picture. This is done with the
<em>subplot</em> command. Matlab can treat the window as an array of plots.
Here we will have one row and two columns giving us two plots. In plot
#1 the function is plotted, while in plot #2 the error is plotted.</p>
<div class="highlight-matlab" id="index-3"><div class="highlight"><pre><span class="o">&gt;&gt;</span> <span class="n">subplot</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="o">&gt;&gt;</span> <span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">,</span><span class="s">&#39;go&#39;</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">true</span><span class="p">)</span>
<span class="o">&gt;&gt;</span> <span class="n">subplot</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">);</span>
<span class="o">&gt;&gt;</span> <span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="nb">abs</span><span class="p">(</span><span class="n">true</span><span class="o">-</span><span class="n">y</span><span class="p">),</span><span class="s">&#39;mx&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="figure align-center" id="id2">
<img alt="Approximation to the differential equation." src="_images/course.gif" />
<p class="caption"><span class="caption-text">Figure 1.</span></p>
<div class="legend">
The two plots from the first approximation.</div>
</div>
<p>Let&#8217;s start over. A new approximation is found by cutting the step
size in half. But first, the picture is completely cleared and reset
using the <em>clf</em> comand. (Note that I am using new vectors <em>x1</em> and
<em>y1</em>.)</p>
<span class="target" id="index-4"></span><div class="highlight-matlab" id="index-5"><div class="highlight"><pre><span class="o">&gt;&gt;</span> <span class="n">clf</span>
<span class="o">&gt;&gt;</span> <span class="n">h</span> <span class="p">=</span> <span class="n">h</span><span class="o">/</span><span class="mi">2</span><span class="p">;</span>
<span class="o">&gt;&gt;</span> <span class="n">x1</span> <span class="p">=</span> <span class="mi">0</span><span class="p">:</span><span class="n">h</span><span class="p">:</span><span class="mi">1</span><span class="p">;</span>
<span class="o">&gt;&gt;</span> <span class="n">y1</span> <span class="p">=</span> <span class="mi">0</span><span class="o">*</span><span class="n">x1</span><span class="p">;</span>
<span class="o">&gt;&gt;</span> <span class="n">y1</span><span class="p">(</span><span class="mi">1</span><span class="p">)</span> <span class="p">=</span> <span class="mi">1</span><span class="p">;</span>
<span class="o">&gt;&gt;</span> <span class="k">for</span> <span class="nb">i</span><span class="p">=</span><span class="mi">2</span><span class="p">:</span><span class="n">max</span><span class="p">(</span><span class="nb">size</span><span class="p">(</span><span class="n">y1</span><span class="p">)),</span>
     <span class="n">y1</span><span class="p">(</span><span class="nb">i</span><span class="p">)</span> <span class="p">=</span> <span class="n">y1</span><span class="p">(</span><span class="nb">i</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="o">+</span> <span class="n">h</span><span class="o">/</span><span class="n">y1</span><span class="p">(</span><span class="nb">i</span><span class="o">-</span><span class="mi">1</span><span class="p">);</span>
   <span class="k">end</span>
<span class="o">&gt;&gt;</span> <span class="n">true1</span> <span class="p">=</span> <span class="nb">sqrt</span><span class="p">(</span><span class="mi">2</span><span class="o">*</span><span class="n">x1</span><span class="o">+</span><span class="mi">1</span><span class="p">);</span>
</pre></div>
</div>
<p>The new approximation is plotted, but be careful! The vectors passed
to <em>plot</em> have to match. The labels are given for the axis and a title
is given to each plot in the following example. The following example
was chosen to show how you can use the <em>subplot</em> command to cycle
through the plots at any time.</p>
<span class="target" id="index-6"></span><span class="target" id="index-7"></span><div class="highlight-matlab" id="index-8"><div class="highlight"><pre><span class="o">&gt;&gt;</span> <span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y1</span><span class="p">,</span><span class="s">&#39;go&#39;</span><span class="p">,</span><span class="n">x</span><span class="p">,</span><span class="n">true1</span><span class="p">)</span>
??? <span class="n">Error</span> <span class="n">using</span> <span class="o">==&gt;</span> <span class="n">plot</span>
<span class="n">Vectors</span> <span class="n">must</span> <span class="n">be</span> <span class="n">the</span> <span class="n">same</span> <span class="n">lengths</span><span class="p">.</span>

<span class="o">&gt;&gt;</span> <span class="n">plot</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span><span class="n">y1</span><span class="p">,</span><span class="s">&#39;go&#39;</span><span class="p">,</span><span class="n">x1</span><span class="p">,</span><span class="n">true1</span><span class="p">)</span>
<span class="o">&gt;&gt;</span> <span class="n">plot</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span><span class="nb">abs</span><span class="p">(</span><span class="n">true1</span><span class="o">-</span><span class="n">y1</span><span class="p">),</span><span class="s">&#39;mx&#39;</span><span class="p">)</span>
<span class="o">&gt;&gt;</span> <span class="n">subplot</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="o">&gt;&gt;</span> <span class="n">plot</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="nb">abs</span><span class="p">(</span><span class="n">true</span><span class="o">-</span><span class="n">y</span><span class="p">),</span><span class="s">&#39;mx&#39;</span><span class="p">)</span>
<span class="o">&gt;&gt;</span> <span class="n">subplot</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">2</span><span class="p">);</span>
<span class="o">&gt;&gt;</span> <span class="n">plot</span><span class="p">(</span><span class="n">x1</span><span class="p">,</span><span class="nb">abs</span><span class="p">(</span><span class="n">true1</span><span class="o">-</span><span class="n">y1</span><span class="p">),</span><span class="s">&#39;mx&#39;</span><span class="p">)</span>
<span class="o">&gt;&gt;</span> <span class="n">title</span><span class="p">(</span><span class="s">&#39;Errors for h=1/32&#39;</span><span class="p">)</span>
<span class="o">&gt;&gt;</span> <span class="n">xlabel</span><span class="p">(</span><span class="s">&#39;x&#39;</span><span class="p">);</span>
<span class="o">&gt;&gt;</span> <span class="n">ylabel</span><span class="p">(</span><span class="s">&#39;|Error|&#39;</span><span class="p">);</span>
<span class="o">&gt;&gt;</span> <span class="n">subplot</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">2</span><span class="p">,</span><span class="mi">1</span><span class="p">);</span>
<span class="o">&gt;&gt;</span> <span class="n">xlabel</span><span class="p">(</span><span class="s">&#39;x&#39;</span><span class="p">);</span>
<span class="o">&gt;&gt;</span> <span class="n">ylabel</span><span class="p">(</span><span class="s">&#39;|Error|&#39;</span><span class="p">);</span>
<span class="o">&gt;&gt;</span> <span class="n">title</span><span class="p">(</span><span class="s">&#39;Errors for h=1/16&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="figure align-center" id="id3">
<img alt="Errors in the approximation to the differential equation." src="_images/error.gif" />
<p class="caption"><span class="caption-text">Figure 2.</span></p>
<div class="legend">
The errors for the two approximations</div>
</div>
<p>Finally, if you want to print the plot, you must first print the plot
to a file. To print a postscript file of the current plot you can use
the <em>print</em> command. The following example creates a postscript file
called error.ps which resides in the current directory. This new file
(error.ps) can be printed from the UNIX prompt using the lpr command.</p>
<span class="target" id="index-9"></span><div class="highlight-matlab" id="index-10"><div class="highlight"><pre><span class="o">&gt;&gt;</span> <span class="n">print</span> <span class="o">-</span><span class="n">dps</span> <span class="n">error</span><span class="p">.</span><span class="n">ps</span>
</pre></div>
</div>
<p>If you wish to print a png file you can use the <em>png</em> option instead:</p>
<div class="highlight-matlab" id="index-11"><div class="highlight"><pre><span class="o">&gt;&gt;</span> <span class="n">print</span> <span class="o">-</span><span class="n">dpng</span> <span class="n">error</span><span class="p">.</span><span class="n">png</span>
</pre></div>
</div>
</div>


           </div>
          </div>
          



  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="execute.html" class="btn btn-neutral float-right" title="Executable Files" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="control.html" class="btn btn-neutral" title="Loops" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
		

		<br><br>


<footer>
		<hr/>

		<div role="contentinfo">
          <center>
          <script async src="../../../pagead2.googlesyndication.com/pagead/js/f.txt"></script>
<!-- matlab tutorial wide -->
<ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-8301099787686694"
     data-ad-slot="8763269985"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</center>


			<p>
			<span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">Matlab Tutorial</span> by <a xmlns:cc="http://creativecommons.org/ns#" href="mailto:kjblack@gmail.com" property="cc:attributionName" rel="cc:attributionURL">Kelly Black</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/">Creative Commons Attribution-NonCommercial 4.0 International License</a> (2015).<br />Based on a work at <a xmlns:dct="http://purl.org/dc/terms/" href="index-2.html" rel="dct:source">http://www.cyclismo.org/tutorial/matlab/</a>.
            </p>

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org/">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'./',
            VERSION:'3.0',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true
        };
    </script>
      <script type="text/javascript" src="_static/jquery.js"></script>
      <script type="text/javascript" src="_static/underscore.js"></script>
      <script type="text/javascript" src="_static/doctools.js"></script>
      <script type="text/javascript" src="../../../cdn.mathjax.org/mathjax/latest/MathJaxdda6.js?config=TeX-AMS-MML_HTMLorMML"></script>

  

  
  
    <script type="text/javascript" src="_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
  
<p>
<a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/"><img alt="Creative Commons License" style="border-width:0" src="../../../licensebuttons.net/l/by-nc/4.0/80x15.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/Text" property="dct:title" rel="dct:type">Matlab Tutorial</span> by <a xmlns:cc="http://creativecommons.org/ns#" href=href%3d_mailto_kjblack%40gmail.html property="cc:attributionName" rel="cc:attributionURL">Kelly Black</a> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc/4.0/">Creative Commons Attribution-NonCommercial 4.0 International License</a>.<br />Based on a work at <a xmlns:dct="http://purl.org/dc/terms/" href="index-2.html" rel="dct:source">http://www.cyclismo.org/tutorial/matlab/</a>.
</p>
<p>This page generated using <a href="http://sphinx-doc.org/">Sphinx</a>.</p>





</body>

<!-- Mirrored from www.cyclismo.org/tutorial/matlab/plot.html by HTTrack Website Copier/3.x [XR&CO'2014], Sat, 15 Jan 2022 04:32:28 GMT -->
</html>